﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;



namespace UIFramwork.Pool
{
    /// <summary>
    /// UI 池中心
    /// </summary>
    public class UIAssetsPool : MonoSingletonBase<UIAssetsPool>
    {
        /// <summary>
        /// UI 资产使用库池
        /// </summary>
        public Dictionary<Type, UIUnitPool> Pool { get; private set; }

        /// <summary>
        /// UI 资产缓存库
        /// </summary>
        public UIAssets UIAssets { get; private set; }



        public UIAssetsPool()
        {
            UIAssets = new UIAssets();
            Pool = new Dictionary<Type, UIUnitPool>();

            Debug.Log($"UIPoolCenter|UI 池初始化结束");
        }


        /// <summary>
        /// 释放池（全部）
        /// </summary>
        public void Dispose()
        {
            Pool.ToList().ForEach(e => e.Value.Dispose());
            Pool.Clear();
        }

        /// <summary>
        /// 释放池（指定）
        /// </summary>
        public void Dispose<T>()
        {
            Type key = typeof(T);

            if (this.Pool.TryGetValue(key, out UIUnitPool output))
            {
                output.Dispose();
            }
        }



        /// <summary>
        /// 获取池| 基于路径资产库
        /// </summary>
        /// <remarks>
        /// 如果该资产存在，则返回该资产对应的管理池。
        /// </remarks>
        public static UIUnitPool GetUnitPool(Type type)
        {
            bool isGet = UIAssetsPool.TryGetUnitPool(type, out UIUnitPool output);

            if (!isGet)
            {
                bool isDone = Instance.TryCreateUIUnitPool(type, out output);
                if (!isDone) return null;
            }



            return output;
        }

        /// <summary>
        /// 创建池
        /// </summary>
        private bool TryCreateUIUnitPool(Type type, out UIUnitPool output)
        {
            // 检查 本地资产库是否存在
            bool isGet = this.UIAssets.TryGetUIAsset(type, out GameObject prefab);
            if (!isGet)
            {
                output = null;
                return false;
            }

            // 初始化 UI生命周期
            bool isUIBase = prefab.TryGetComponent(out UIBase ui);
            if (isUIBase)
            {
                ui.CreateUI();
                ui.StartUI();              
            }

            // 创建 池区域
            output = UIAssetsPool.CreateUnitPool(type, prefab);

            // 更新 索引库
            this.Pool.TryAdd(type, output);

            return output;
        }





        /// <summary>
        /// 获取池| 基于随机创建资产
        /// </summary>
        /// <param name="type"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static bool TryGetUnitPool(Type type, out UIUnitPool output)
        {
            return Instance.Pool.TryGetValue(type, out output);
        }

        /// <summary>
        /// 创建池| 基于随机创建资产
        /// </summary>
        /// <returns></returns>
        public static UIUnitPool CreateUnitPool(Type type, GameObject prefab)
        {
            UIUnitPool unitPool = new GameObject($"{type}").AddComponent<UIUnitPool>();
            {
                unitPool.gameObject.transform.SetParent(Instance.transform);
                unitPool.transform.localScale = Vector3.one;
                unitPool.transform.localRotation = Quaternion.identity;
                unitPool.transform.position = Vector3.zero;
            };

            unitPool.Set(type);
            unitPool.Set(prefab);

            return unitPool;
        }
    }
}
